package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisMessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrdersettingDao ordersettingDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Result addOrder(Map<String, Object> map) throws Exception {
        String telephone = String.valueOf(map.get("telephone"));
        String name = String.valueOf(map.get("name"));
        String sex = String.valueOf(map.get("sex"));
        String idCard = String.valueOf(map.get("idCard"));
        Integer setmealId = Integer.parseInt(String.valueOf(map.get("setmealId")));
        String orderType = String.valueOf(map.get("orderType"));

        String orderDateStr = String.valueOf(map.get("orderDate"));

        Date orderDate = new SimpleDateFormat("yyyy-MM-dd").parse(orderDateStr);
        //1.该日期是否进行了预约设置
        OrderSetting orderSetting = ordersettingDao.findOrderSettingByDate(orderDate);
        if (orderSetting == null) {
            //没有进行预约设置
            return new Result(false, MessageConst.GET_ORDERSETTING_FAIL);
        }

        //已进行了预约设置
        //2.判断该日期是否预约已满
        int reservations = orderSetting.getReservations();
        int number = orderSetting.getNumber();
        if (reservations >= number) {
            //预约已满,不可以进行预约
            return new Result(false, MessageConst.ORDER_FULL);
        }

        //3.判断该用户是否是会员
        Member member = memberDao.findByTelephone(telephone);
        if (member == null) {
            //不是会员,自动注册为会员(注册会员,回显主键)
            //创建会员对象
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            //自动注册为会员
            memberDao.add(member);
        } else {
            //4.判断是否重复预约
            //条件:该会员(memberId),该日期(orderDate),该套餐(setmealId)
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setSetmealId(setmealId);

            long count = orderDao.getByOrder(order);
            if (count > 0) {
                return new Result(false, MessageConst.HAS_ORDERED);
            }
        }
        //5.添加预约信息,访问dao
        //创建预约对象
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);

        //添加预约对象
        orderDao.addOrder(order);

        //6.预约完成后,更新已预约的人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        ordersettingDao.edit(orderSetting);
        return new Result(true,MessageConst.ORDER_SUCCESS,order);
    }


    @Override
    public Map<String, Object> findById(Integer orderId) {
        return orderDao.findById(orderId);
    }
}
